package cn.lystudio.rohmtimeopwide_background_v11.controller.appApi;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.lystudio.rohmtimeopwide_background_v11.entity.dto.MachineDTO;
import cn.lystudio.rohmtimeopwide_background_v11.entity.dto.UserDepartmentDTO;
import cn.lystudio.rohmtimeopwide_background_v11.entity.po.DepartmentsPO;
import cn.lystudio.rohmtimeopwide_background_v11.entity.po.MachinePO;
import cn.lystudio.rohmtimeopwide_background_v11.service.DepartmentService;
import cn.lystudio.rohmtimeopwide_background_v11.service.MachineService;
import cn.lystudio.rohmtimeopwide_background_v11.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/app-api/machine")
@CrossOrigin
@SaIgnore
@Slf4j
public class AppMachineController {
    private MachineService machineService;
    private UserService userService;
    private DepartmentService departmentService;

    @Autowired
    @Lazy
    public void setDepartmentService(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    @Autowired
    @Lazy
    public void setMachineService(MachineService machineService) {
        this.machineService = machineService;
    }

    @Autowired
    @Lazy
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/getMachineNoEcho")
    public SaResult getMachineNoEcho(String machineNo, String userCode) {
        MachinePO machinePO = machineService.get1ByMachineNo_Usable(machineNo);
        if (ObjectUtil.isEmpty(machinePO)) return SaResult.error("设备状态异常");

        UserDepartmentDTO userDept = userService.get1UserDepartment_Usable(userCode);
        if (ObjectUtil.isEmpty(userDept)) return SaResult.error("用户权限异常");

        List<DepartmentsPO> allUserSecDept = departmentService.getAllSecDeptByTopDeptID(userDept.getDepartmentsPO().getDepartmentID());
        if (CollUtil.isEmpty(allUserSecDept)) return SaResult.error("用户权限异常");
        List<Integer> secDeptIDList = CollUtil.list(false);
        CollUtil.forEach(allUserSecDept, (secDept, index) -> {
            secDeptIDList.add(secDept.getDepartmentID());
        });
        if (!secDeptIDList.contains(machinePO.getSecDeptID())) return SaResult.error("用户无此设备权限");
        return SaResult.data(machinePO.getMachineNo());
    }

    @GetMapping("/getMachineNoEcho3")
    public SaResult getMachineNoEcho3(String machineNo, String userCode) {
        MachineDTO machineDTO = machineService.getMachineDTOByMachineNo(machineNo);
        if (ObjectUtil.isEmpty(machineDTO)) return SaResult.error("设备状态异常");

        UserDepartmentDTO userDept = userService.get1UserDepartment_Usable(userCode);
        if (ObjectUtil.isEmpty(userDept)) return SaResult.error("用户权限异常");

        List<DepartmentsPO> allUserSecDept = departmentService.getAllSecDeptByTopDeptID(userDept.getDepartmentsPO().getDepartmentID());
        if (CollUtil.isEmpty(allUserSecDept)) return SaResult.error("用户权限异常");

        List<Integer> secDeptIDList = CollUtil.list(false);
        CollUtil.forEach(allUserSecDept, (secDept, index) -> {
            secDeptIDList.add(secDept.getDepartmentID());
        });
        if (!secDeptIDList.contains(machineDTO.getMachine().getSecDeptID())) return SaResult.error("用户无此设备权限");

        return SaResult.data(machineDTO);
    }
    @GetMapping("/getMachineNoEcho2")
    public SaResult getMachineNoEcho2(String machineNo){
        MachinePO machinePO = machineService.get1ByMachineNo_Usable(machineNo);
        if(ObjectUtil.isEmpty(machinePO)) return SaResult.error("设备状态异常");
        return SaResult.data(machinePO);
    }

    /**
     * 获取半田的设备
     * @param machineNo
     * @return
     */
    @GetMapping("/getMachine4SSD1BT")
    public SaResult getMachine4SSD1BT(String machineNo){
        MachinePO machinePO = machineService.getMachine4SSD1BT(machineNo);
        if(ObjectUtil.isEmpty(machinePO)) return SaResult.error("设备状态异常");
        return SaResult.ok();
    }
    @GetMapping("/getMachine4SSD1ZL")
    public SaResult getMachine4SSD1ZL(String machineNo){
        MachinePO machinePO = machineService.getMachine4SSD1ZL(machineNo);
        if(ObjectUtil.isEmpty(machinePO)) return SaResult.error("设备状态异常");
        return SaResult.ok();
    }
    @GetMapping("/getMachine4SSD1CD")
    public SaResult getMachine4SSD1CD(String machineNo){
        MachinePO machinePO = machineService.getMachine4SSD1CD(machineNo);
        if(ObjectUtil.isEmpty(machinePO)) return SaResult.error("设备状态异常");
        return SaResult.ok();
    }

    /**
     * 获取USM组立设备List
     * @return
     */
    @GetMapping("/getUSMZuLiMachineList")
    public SaResult getUSMZuLiMachineList(){
        List<MachinePO> usmZuLiMachineList = machineService.getUSMZuLiMachineList();
        if(CollUtil.isEmpty(usmZuLiMachineList)) return SaResult.error("未找到组立设备");
        return SaResult.data(usmZuLiMachineList);
    }

    @GetMapping("/getUSMBtMachineList")
    public SaResult getUSMBtMachineList(){
        List<MachinePO> usmBtMachineList = machineService.getUSMBtMachineList();
        if(CollUtil.isEmpty(usmBtMachineList)) return SaResult.error("未找到组立设备");
        return SaResult.data(usmBtMachineList);
    }
}
